package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBeen;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;


//    @Override
//    public void addemp(Emp emp) {
//        emp.setPassword("123777");
//        emp.setCreateTime(LocalDateTime.now());
//        emp.setUpdateTime(LocalDateTime.now());
//        empMapper.addemp(emp);
//    }
//
//
//
//    @Override
//    public PageBeen sletepage(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//
//        PageHelper.startPage(page,pageSize);
//        //2.调用mapper的page方法
//        Page<Emp>list= empMapper.page(name, gender, begin, end);
//        //3.封装到pagebeen对象
//        PageBeen pageBeen = new PageBeen(list.getTotal(), list.getResult());
//
//        return pageBeen;
//    }
//
//}

    @Override
    public PageBeen sletepage(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//        //1、获取总记录数
//        Long count = empMapper.count();
//
//        //2、获取分页查询结果列表
//        Integer start = (page - 1) * pageSize; //计算起始索引 , 公式: (页码-1)*页大小
//        List<Emp> empList = empMapper.list(start, pageSize);
//
//        //3、封装PageBean对象
//        PageBeen pageBeen = new PageBeen(count,empList);
//        return  pageBeen;

        //1.开启分页查询
        PageHelper.startPage(page, pageSize);
        //2.调用mapper的page方法
        Page<Emp> list = empMapper.page(name, gender, begin, end);
        //3.封装到pagebeen对象
        PageBeen pageBeen = new PageBeen(list.getTotal(), list.getResult());

        return pageBeen;

    }

    @Override
    public void add(Emp emp) {
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);

    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.fistById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public void daleteById(Integer id) {
        empMapper.daleteById(id);
    }

    @Override
    public Emp login(Emp emp) {
//        //调用dao层功能：登录
         Emp loginEmp  = empMapper.getbByUsernameAndPassword(emp);
        return loginEmp;
    }



}

